package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.StudentClassBean;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.clazz.bean.TeacherClassBean;
import com.ciwong.epaper.modules.epaper.ui.TeaPublishWorkActivity;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;

import java.util.List;


public class PublishWorkClassNameAdapter extends BaseAdapter {
    //老师的班级集合
    List<TeacherClassBean> teacherClassBeans;

    //班级集合
    private List<StudentClassBean> studentClassBeanList;
    //学生信息集合
    private List<StudentGroupBean> studentBeanLists;

    private Context mContext;

    private int JUMP_TO_CHOOSE_GROUP = 10001;

    public PublishWorkClassNameAdapter(List<TeacherClassBean> teacherClassList, List<StudentClassBean> studentClassBeanList, List<StudentGroupBean> studentBeanLists, Context mContext) {
        this.teacherClassBeans = teacherClassList;
        this.mContext = mContext;
        this.studentClassBeanList = studentClassBeanList;
        this.studentBeanLists = studentBeanLists;
    }

    @Override
    public int getCount() {
        if (null != teacherClassBeans && teacherClassBeans.size() > 0) {
            return teacherClassBeans.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if (null != teacherClassBeans && teacherClassBeans.size() > 0) {
            return teacherClassBeans.get(i);
        } else {
            return null;
        }

    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final int index = i;
        final PublishViewHolder viewHolder;
        if (null == view) {
            viewHolder = new PublishViewHolder();
            view = View.inflate(mContext, R.layout.activity_class_list_item, null);
            viewHolder.class_name_check = view.findViewById(R.id.class_name_check);
            viewHolder.class_name_text = view.findViewById(R.id.class_name_text);
            viewHolder.class_count_text = view.findViewById(R.id.class_count_text);
            viewHolder.class_name_go = view.findViewById(R.id.class_name_go);
            viewHolder.class_name_go_lay = view.findViewById(R.id.class_name_go_lay);
            viewHolder.class_name_check_lay = view.findViewById(R.id.class_name_check_lay);
            view.setTag(viewHolder);
        } else {
            viewHolder = (PublishViewHolder) view.getTag();
        }

        viewHolder.class_name_go_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (null != studentClassBeanList) {
                    for (int j = 0; j < studentClassBeanList.size(); j++) {
                        StudentClassBean studentClassBean = studentClassBeanList.get(j);
                        for (int a = 0; a < studentClassBean.getStudentClassList().size(); a++) {
                            if (studentClassBean.getStudentClassList().get(a).getClassId() == teacherClassBeans.get(i).getClassId()) {
                                studentBeanLists = studentClassBean.getStudentClassList();
                            }
                        }
                    }
                }

                EpaperJumpManager.jumpToChooseGroupActivity((Activity) mContext, teacherClassBeans.get(i).getClassId(), teacherClassBeans.get(i).getChecked(), teacherClassBeans.get(i).getClassName(), studentBeanLists, JUMP_TO_CHOOSE_GROUP);
            }
        });

        if (null != teacherClassBeans && teacherClassBeans.size() > 0) {
            /*viewHolder.class_name_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                    if (!compoundButton.isPressed()){
                        return;
                    }
                    if (isCheck){
                        viewHolder.class_name_check.setChecked(true);
                        viewHolder.class_name_check.setSelected(true);
                        viewHolder.class_count_text.setText("("+teacherClassBeans.get(i).getStudentSize()+"/"+teacherClassBeans.get(i).getStudentSize()+")");
                        teacherClassBeans.get(index).setChecked(1);
                        if (mContext instanceof TeaPublishWorkActivity){
                            ((TeaPublishWorkActivity)mContext).setUserList(isCheck,teacherClassBeans.get(index));
                        }
                    }else {
                        viewHolder.class_name_check.setChecked(false);
                        viewHolder.class_name_check.setSelected(true);
                        viewHolder.class_count_text.setText("(0/"+teacherClassBeans.get(i).getStudentSize()+")");
                        teacherClassBeans.get(index).setChecked(2);
                        if (mContext instanceof TeaPublishWorkActivity){
                            ((TeaPublishWorkActivity)mContext).setUserList(isCheck,teacherClassBeans.get(index));
                        }
                    }
                }
            });*/

            viewHolder.class_name_check_lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (teacherClassBeans.get(i).getClassId() == 0) {
                        updateAllSel(viewHolder, i);
                    } else {
                        if (!viewHolder.class_name_check.isChecked()) {
                            viewHolder.class_name_check.setChecked(true);
                            viewHolder.class_name_check.setSelected(true);
                            viewHolder.class_count_text.setText("(" + teacherClassBeans.get(i).getStudentSize() + "/" + teacherClassBeans.get(i).getStudentSize() + ")");
                            teacherClassBeans.get(index).setChecked(1);
                            if (mContext instanceof TeaPublishWorkActivity) {
                                ((TeaPublishWorkActivity) mContext).setUserList(true, teacherClassBeans.get(index));
                            }
                        } else {
                            viewHolder.class_name_check.setChecked(false);
                            viewHolder.class_name_check.setSelected(true);
                            viewHolder.class_count_text.setText("(0/" + teacherClassBeans.get(i).getStudentSize() + ")");
                            teacherClassBeans.get(index).setChecked(2);
                            if (mContext instanceof TeaPublishWorkActivity) {
                                ((TeaPublishWorkActivity) mContext).setUserList(false, teacherClassBeans.get(index));
                            }
                        }
                    }
                }
            });
            if (teacherClassBeans.get(index).getChecked() == 1) {
                viewHolder.class_name_check.setChecked(true);
                viewHolder.class_name_check.setSelected(true);
            } else if (teacherClassBeans.get(index).getChecked() == 0) {
                viewHolder.class_name_check.setChecked(false);
                viewHolder.class_name_check.setSelected(true);
            } else if (teacherClassBeans.get(index).getChecked() == 2) {
                viewHolder.class_name_check.setChecked(false);
                viewHolder.class_name_check.setSelected(true);
            } else {
                viewHolder.class_name_check.setChecked(true);
                viewHolder.class_name_check.setSelected(false);
            }
            TeacherClassBean teacherClassBean = teacherClassBeans.get(i);
            viewHolder.class_name_text.setText(teacherClassBean.getClassName());
            if (teacherClassBean.getClassId() == 0) {
                viewHolder.class_count_text.setVisibility(View.GONE);
                viewHolder.class_name_go_lay.setVisibility(View.GONE);
            } else {
                viewHolder.class_count_text.setVisibility(View.VISIBLE);
                viewHolder.class_name_go_lay.setVisibility(View.VISIBLE);
                if (viewHolder.class_name_check.isChecked()){
                    viewHolder.class_count_text.setText("(" + teacherClassBeans.get(i).getStudentSize() + "/" + teacherClassBeans.get(i).getStudentSize() + ")");
                }else {
                    viewHolder.class_count_text.setText("(0/" + teacherClassBeans.get(i).getStudentSize() + ")");
                }
            }
        }
        return view;
    }

    private void updateAllSel(PublishViewHolder viewHolder, int i) {
        if (!viewHolder.class_name_check.isChecked()) {
            for (int j = 0; j < teacherClassBeans.size(); j++) {
                teacherClassBeans.get(j).setChecked(1);
                if (mContext instanceof TeaPublishWorkActivity) {
                    ((TeaPublishWorkActivity) mContext).setUserList(true, teacherClassBeans.get(j));
                }
            }
        }else {
            for (int j = 0; j < teacherClassBeans.size(); j++) {
                teacherClassBeans.get(j).setChecked(2);
                if (mContext instanceof TeaPublishWorkActivity) {
                    ((TeaPublishWorkActivity) mContext).setUserList(false, teacherClassBeans.get(j));
                }
            }
        }

        notifyDataSetChanged();
    }

    public static class PublishViewHolder {
        private CheckBox class_name_check;
        private TextView class_name_text;
        private TextView class_count_text;
        private ImageView class_name_go;
        private RelativeLayout class_name_go_lay;
        private RelativeLayout class_name_check_lay;

    }

    public void setNewData(List<TeacherClassBean> teacherClassBeans) {
        this.teacherClassBeans = teacherClassBeans;
        this.notifyDataSetChanged();
    }


}
